package com.zhuss.order.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.appconfig.Cache;
import com.zhuss.bean.bo.order.order.CreateOrderBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.ShopException;
import com.zhuss.order.db.dao.ext.OrderDao;
import com.zhuss.order.db.entity.Order;
import com.zhuss.product.db.dao.ext.ProductDao;
import com.zhuss.util.JsonUtil;


/**
 * @description: 订单创建
 * @author: zhushanshuo
 * @create: Wed Mar 24 22:32:50 CST 2021
 **/
@Component("orderCreate")
public class OrderCreate extends AbstractProcessor<Object, CreateOrderBo> implements Processor<Object, CreateOrderBo> {

    @Autowired
    private OrderDao orderDao;
    
    @Autowired
	@Qualifier("orderRead")
	private OrderRead orderRead;
    
    @Autowired
	private ProductDao productDao;
    
    @Autowired
	@Qualifier("usedCache")
	private Cache<String, String> usedCache;
    
    
    /**
     * 校验
     */
    @Override
    public void check(CreateOrderBo bo) throws ShopException {
    	// TODO 创建 订单 校验， 请补充
    }
    
    /**
     * 操作方法
     * @throws ShopException 
     */
    @Override
    public Object process(CreateOrderBo param) throws ShopException {
    	/*Long productId = param.getProductId();
    	
    	long influnce = usedCache.incr(productId + "", 1);
    	System.out.println(influnce);*/
    	// 扣减商品库存 ，  返回受影响数量
    	Long productId = param.getProductId();
		Integer influnce = productDao.used(productId, 1l);
		if (influnce.equals(0)) {
			throw new ShopException(ExceptionCode.库存不足);
		}
    	
    	Order record = new Order();
    	record.setOrderNo(param.getOrderNo());
    	record.setOrderSequence(param.getOrderSequence());
    	record.setCompanyId(param.getCompanyId());
    	record.setProductId(param.getProductId());
    	record.setCount(param.getCount());
    	record.setAmount(param.getAmount());
    	record.setComment(param.getComment());
    	record.setStatus(param.getStatus());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	orderDao.create(record);
    	param.setTargetId(record.getOrderId());
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CreateOrderBo param) throws ShopException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.订单模块.key());
    	record.setAction(LogActionEnum.订单创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getOrderSequence());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	//log(record);
    	return null;
	}
}
